package com.nancy.springioc.context.support;

import com.nancy.springioc.bean.factory.support.BeanDefinitionReader;
import com.nancy.springioc.bean.factory.support.BeanDefinitionRegistry;

import java.util.HashMap;
import java.util.Map;

/**
 * IOC容器抽象类，实现ApplicationContext接口，加载bean
 */
public abstract class AbstractApplicationContext implements ApplicationContext {
    //声明一个map对象，存储bean
    public Map<String, Object> singletonBeanMap = new HashMap<>();

    //声明解析器
    public BeanDefinitionReader beanDefinitionReader;

    //声明配置文件的路径
    public String configLocation;

    @Override
    public void refresh() throws Exception {
        //加载配置文件
        beanDefinitionReader.loadBeanDefinitions(configLocation);
        //初始化bean
        initBean();
    }

    //初始化bean
    public void initBean() throws Exception {
        //获取注册表
        BeanDefinitionRegistry registry = beanDefinitionReader.getRegistry();
        //获取BendDefinition对象
        String[] beanDefinitionNames = registry.getBeanDefinitionNames();
        //遍历beanDefinitionNames
        for (String beanName : beanDefinitionNames) {
            //获取bean对象
            getBean(beanName);
        }

    }
}
